home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJINC106.ARJ / _COMPLEX.H next >
C/C++ Source or Header  |  1992-03-29  |  7KB  |  277 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _Complex_h
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #endif
  23. #define _Complex_h 1
  24.  
  25.  
  26. #include <stream.h>
  27. #include <math.h>
  28.  
  29. class Complex
  30. {
  31. #ifdef __ATT_complex__
  32. public:
  33. #else
  34. protected:
  35. #endif
  36.  
  37.   double           re;
  38.   double           im;
  39.  
  40. public:
  41.  
  42.   double           real() const;
  43.   double           imag() const;
  44.  
  45.                    Complex();
  46.                    Complex(const Complex& y);
  47.                    Complex(double r, double i=0);
  48.  
  49.                   ~Complex();
  50.  
  51.   Complex&         operator =  (const Complex& y);
  52.  
  53.   Complex&         operator += (const Complex& y);
  54.   Complex&         operator += (double y);
  55.   Complex&         operator -= (const Complex& y);
  56.   Complex&         operator -= (double y);
  57.   Complex&         operator *= (const Complex& y);
  58.   Complex&         operator *= (double y);
  59.  
  60.   Complex&         operator /= (const Complex& y); 
  61.   Complex&         operator /= (double y); 
  62.  
  63.   void             error(const char* msg) const;
  64. };
  65.  
  66.  
  67. // non-inline functions
  68.  
  69. Complex   operator /  (const Complex& x, const Complex& y);
  70. Complex   operator /  (const Complex& x, double y);
  71. Complex   operator /  (double   x, const Complex& y);
  72.  
  73. Complex   cos(const Complex& x);
  74. Complex   sin(const Complex& x);
  75.  
  76. Complex   cosh(const Complex& x);
  77. Complex   sinh(const Complex& x);
  78.  
  79. Complex   exp(const Complex& x);
  80. Complex   log(const Complex& x);
  81.  
  82. Complex   pow(const Complex& x, int p);
  83. Complex   pow(const Complex& x, const Complex& p);
  84. Complex   pow(const Complex& x, double y);
  85. Complex   sqrt(const Complex& x);
  86.    
  87. istream&  operator >> (istream& s, Complex& x);
  88. ostream&  operator << (ostream& s, const Complex& x);
  89.  
  90. // other functions defined as inlines
  91.  
  92. int  operator == (const Complex& x, const Complex& y);
  93. int  operator == (const Complex& x, double y);
  94. int  operator != (const Complex& x, const Complex& y);
  95. int  operator != (const Complex& x, double y);
  96.  
  97. Complex  operator - (const Complex& x);
  98. Complex  conj(const Complex& x);
  99. Complex  operator + (const Complex& x, const Complex& y);
  100. Complex  operator + (const Complex& x, double y);
  101. Complex  operator + (double x, const Complex& y);
  102. Complex  operator - (const Complex& x, const Complex& y);
  103. Complex  operator - (const Complex& x, double y);
  104. Complex  operator - (double x, const Complex& y);
  105. Complex  operator * (const Complex& x, const Complex& y);
  106. Complex  operator * (const Complex& x, double y);
  107. Complex  operator * (double x, const Complex& y);
  108.  
  109. double  real(const Complex& x);
  110. double  imag(const Complex& x);
  111. double  abs(const Complex& x);
  112. double  norm(const Complex& x);
  113. double  arg(const Complex& x);
  114.  
  115. Complex  polar(double r, double t = 0.0);
  116.  
  117.  
  118. // inline members
  119.  
  120. inline double  Complex::real() const { return re; }
  121. inline double  Complex::imag() const { return im; }
  122.  
  123. inline Complex::Complex() {}
  124. inline Complex::Complex(const Complex& y) :re(y.real()), im(y.imag()) {}
  125. inline Complex::Complex(double r, double i) :re(r), im(i) {}
  126.  
  127. inline Complex::~Complex() {}
  128.  
  129. inline Complex&  Complex::operator =  (const Complex& y) 
  130.   re = y.real(); im = y.imag(); return *this; 
  131.  
  132. inline Complex&  Complex::operator += (const Complex& y)
  133.   re += y.real();  im += y.imag(); return *this; 
  134. }
  135.  
  136. inline Complex&  Complex::operator += (double y)
  137.   re += y; return *this; 
  138. }
  139.  
  140. inline Complex&  Complex::operator -= (const Complex& y)
  141.   re -= y.real();  im -= y.imag(); return *this; 
  142. }
  143.  
  144. inline Complex&  Complex::operator -= (double y)
  145.   re -= y; return *this; 
  146. }
  147.  
  148. inline Complex&  Complex::operator *= (const Complex& y)
  149. {  
  150.   double r = re * y.real() - im * y.imag();
  151.   im = re * y.imag() + im * y.real(); 
  152.   re = r; 
  153.   return *this; 
  154. }
  155.  
  156. inline Complex&  Complex::operator *= (double y)
  157. {  
  158.   re *=  y; im *=  y; return *this; 
  159. }
  160.  
  161.  
  162. //  functions
  163.  
  164. inline int  operator == (const Complex& x, const Complex& y)
  165. {
  166.   return x.real() == y.real() && x.imag() == y.imag();
  167. }
  168.  
  169. inline int  operator == (const Complex& x, double y)
  170. {
  171.   return x.imag() == 0.0 && x.real() == y;
  172. }
  173.  
  174. inline int  operator != (const Complex& x, const Complex& y)
  175. {
  176.   return x.real() != y.real() || x.imag() != y.imag();
  177. }
  178.  
  179. inline int  operator != (const Complex& x, double y)
  180. {
  181.   return x.imag() != 0.0 || x.real() != y;
  182. }
  183.  
  184. inline Complex  operator - (const Complex& x)
  185. {
  186.   return Complex(-x.real(), -x.imag());
  187. }
  188.  
  189. inline Complex  conj(const Complex& x)
  190. {
  191.   return Complex(x.real(), -x.imag());
  192. }
  193.  
  194. inline Complex  operator + (const Complex& x, const Complex& y)
  195. {
  196.   return Complex(x.real() + y.real(), x.imag() + y.imag());
  197. }
  198.  
  199. inline Complex  operator + (const Complex& x, double y)
  200. {
  201.   return Complex(x.real() + y, x.imag());
  202. }
  203.  
  204. inline Complex  operator + (double x, const Complex& y)
  205. {
  206.   return Complex(x + y.real(), y.imag());
  207. }
  208.  
  209. inline Complex  operator - (const Complex& x, const Complex& y)
  210. {
  211.   return Complex(x.real() - y.real(), x.imag() - y.imag());
  212. }
  213.  
  214. inline Complex  operator - (const Complex& x, double y)
  215. {
  216.   return Complex(x.real() - y, x.imag());
  217. }
  218.  
  219. inline Complex  operator - (double x, const Complex& y)
  220. {
  221.   return Complex(x - y.real(), -y.imag());
  222. }
  223.  
  224. inline Complex  operator * (const Complex& x, const Complex& y)
  225. {
  226.   return Complex(x.real() * y.real() - x.imag() * y.imag(), 
  227.                  x.real() * y.imag() + x.imag() * y.real());
  228. }
  229.  
  230. inline Complex  operator * (const Complex& x, double y)
  231. {
  232.   return Complex(x.real() * y, x.imag() * y);
  233. }
  234.  
  235. inline Complex  operator * (double x, const Complex& y)
  236. {
  237.   return Complex(x * y.real(), x * y.imag());
  238. }
  239.  
  240. inline double  real(const Complex& x)
  241. {
  242.   return x.real();
  243. }
  244.  
  245. inline double  imag(const Complex& x)
  246. {
  247.   return x.imag();
  248. }
  249.  
  250. inline double  abs(const Complex& x)
  251. {
  252.   return hypot(x.real(), x.imag());
  253. }
  254.  
  255. inline double  norm(const Complex& x)
  256. {
  257.   return (x.real() * x.real() + x.imag() * x.imag());
  258. }
  259.  
  260. inline double  arg(const Complex& x)
  261. {
  262.   return atan2(x.imag(), x.real());
  263. }
  264.  
  265. inline Complex  polar(double r, double t)
  266. {
  267.   return Complex(r * cos(t), r * sin(t));
  268. }
  269.  
  270. #endif
  271.